package com.hmall.gateway.filter;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;

import java.util.List;

//@Component
@Slf4j
public class PrintAnyGatewayFilterFactory extends AbstractGatewayFilterFactory<PrintAnyGatewayFilterFactory.Config> {
    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> {
            //获取请求
            ServerHttpRequest request = exchange.getRequest();
            //编写过滤器逻辑
            log.info("过滤器执行了");
            //放行
            return chain.filter(exchange);
        };
    }


    /**
     * 定义一个对象（泛型）
     */
    @Data
    static class Config{
        private String name;
        private String city;
        private String sex;
    }

    //将变量依次返回（按顺序）
    @Override
    public List<String> shortcutFieldOrder() {
        return List.of("name","city","sex");
    }

    //返回类型（字节码）
    @Override
    public Class<Config> getConfigClass() {
        return Config.class;
    }
}
